﻿using Caliburn.Micro;
using MusicExplorer.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace MusicExplorer.ViewModels
{
    public abstract class PageViewModelBase : Screen
    {
        protected readonly CancellationTokenSource m_tokenSource;
        protected readonly INavigationService m_navigationService;

        public PageViewModelBase(INavigationService navigationService)
        {
            m_tokenSource = new CancellationTokenSource();
            m_navigationService = navigationService;
        }

        public virtual void GoBack()
        {
            m_tokenSource.Cancel();
            m_navigationService.GoBack();
        }

        public event EventHandler HideLoadingEvent = delegate { };
        public event EventHandler ShowLoadingEvent = delegate { };

        public void ShowLoading()
        {
            ShowLoadingEvent(this, EventArgs.Empty);
        }

        public void HideLoading()
        {
            HideLoadingEvent(this, EventArgs.Empty);
        }

        public virtual bool CanGoBack { get { return m_navigationService.CanGoBack; } }

        public NavigationObject Parameter { get; set; }

        public RootViewModel Root { get { return Parameter != null ? Parameter.Root : null; } }

        private string m_title;
        public string Title
        {
            get { return m_title; }
            set
            {
                if(m_title != value)
                {
                    m_title = value;
                    NotifyOfPropertyChange(() => Title);
                    NotifyOfPropertyChange(() => CompleteTitle);
                }
            }
        }

        public virtual string CompleteTitle
        {
            get
            {
                if (Parameter != null && Parameter.SourcePage != null)
                {
                    return string.Format("{0} > {1}", Parameter.SourcePage.CompleteTitle, Title);
                }
                else return Title;
            }
        }

        private bool m_isViewReady = false;
        private Exception m_exception;
        protected void RedirectToError(Exception ex)
        {
            m_exception = ex;
            if(m_isViewReady)
            {
                RedirectToError();
            }
            else
            {
                m_exception = ex;
            }
        }

        private void RedirectToError()
        {
            m_navigationService.NavigateToViewModel<ErrorPageViewModel>(new ErrorPageNavigationObject()
            {
                Root = Root,
                SourcePage = this,
                OldNavigationObject = Parameter,
                OldPageType = GetType()
            });
        }
        
        protected override void OnViewReady(object view)
        {
            if (m_exception != null)
            {
                RedirectToError();
            }
            m_isViewReady = true;
        }
    }
}
